﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Bass.Tools;
using MySql.Data.MySqlClient;

public partial class productdetail : System.Web.UI.Page 
{
    protected string productnum1;
    protected string producttitle1;
    protected string productprice1;
    protected string introduction1;
    protected string producturl1;
    protected string productpicaddr1;

    protected void Page_Load(object sender, EventArgs e)
    {
        iconselected.Attributes.Add("onmouseover", "this.src='image/icon2.jpg'");//鼠标移上去更换新的图片
        iconselected.Attributes.Add("onmouseout", "this.src='image/icon.jpg'");//鼠标移开回复原来图片 
        pinglun.Attributes.Add("onmouseover", "this.src='image/pinglun2.jpg'");//鼠标移上去更换新的图片
        pinglun.Attributes.Add("onmouseout", "this.src='image/pinglun.jpg'");//鼠标移开回复原来图片 

        /*
        if (Convert.ToString(Session["username"]) == "")
        {
            panelcomment.Visible = false;
            commentalert.Visible = true;
        }
        else 
        {
            panelcomment.Visible = true;
            commentalert.Visible = false;
        }

        */
        string productid = Request.QueryString["productid"];
        int productbeclicked;

        if ((productid == null) || (productid == ""))
        {
            Response.Write("<script>alert('产品代码为空-页面将跳转回主页！');location.href='index.aspx';</script>");
            Response.End();
            //Response.Redirect("index.aspx");
        }

        //取到连接字符串
        string propertiesPath = Server.MapPath("mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        MySqlDataAdapter da = new MySqlDataAdapter("select * from product_info where productnum='" + productid + "'", myConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "product_detail");
        DataRowView rowview = ds.Tables["product_detail"].DefaultView[0];

        ltlTitle.Text = rowview["producttitle"].ToString();
        this.producttitle.Text = rowview["producttitle"].ToString();
        this.productprice.Text = rowview["productprice"].ToString();
        this.productkind.Text = rowview["productkind"].ToString();
        this.addtime.Text = ReformDate(rowview["addtime"].ToString());
        this.introduction.Text = rowview["introduction"].ToString();
        this.introduction2.Text = rowview["introduction2"].ToString();
        this.introduction3.Text = rowview["introduction3"].ToString();
        this.introduction4.Text = rowview["introduction4"].ToString();
        this.introduction5.Text = rowview["introduction5"].ToString();
        this.introduction6.Text = rowview["introduction6"].ToString();
        this.introduction7.Text = rowview["introduction7"].ToString();
        this.introduction8.Text = rowview["introduction8"].ToString();
        this.introduction9.Text = rowview["introduction9"].ToString();
        this.introduction10.Text = rowview["introduction10"].ToString();
        this.HyperLink1.NavigateUrl = rowview["producturl"].ToString();
        this.Image4.ImageUrl = rowview["picaddr"].ToString();
        this.HyperLink2.NavigateUrl = rowview["producturl"].ToString();
        this.HyperLink3.NavigateUrl = rowview["producturl"].ToString();
        this.HyperLink4.NavigateUrl = rowview["producturl"].ToString();
        this.HyperLink5.NavigateUrl = rowview["producturl"].ToString();
        this.HyperLink6.NavigateUrl = rowview["producturl"].ToString();
        this.HyperLink7.NavigateUrl = rowview["producturl"].ToString();
        this.HyperLink8.NavigateUrl = rowview["producturl"].ToString();
        this.productsource.Text = rowview["source"].ToString();
        this.detail.Text = rowview["detail"].ToString();
        this.detail2.Text = rowview["detail2"].ToString();
        this.detail3.Text = rowview["detail3"].ToString();
        this.detail4.Text = rowview["detail4"].ToString();
        this.detail5.Text = rowview["detail5"].ToString();

        productnum1 = rowview["productnum"].ToString();
        producttitle1=rowview["producttitle"].ToString();
        productprice1=rowview["productprice"].ToString();
        introduction1=rowview["introduction"].ToString();
        producturl1 = rowview["producturl"].ToString();
        productpicaddr1=rowview["picaddr"].ToString();

        productbeclicked = Convert.ToInt32(rowview["beclickedtotalnumber"])+1;


        if ((rowview["detailurl"].ToString() == "") || (rowview["detailurl"].ToString() == null))
        {
            this.detailimgurl.Visible = false;
        }
        else
        {
            this.detailimgurl.ImageUrl = rowview["detailurl"].ToString();

        }
        if ((rowview["detailurl2"].ToString() == "") || (rowview["detailurl2"].ToString() == null))
        {
            this.detailimgurl2.Visible = false;
        }
        else
        {
            this.detailimgurl2.ImageUrl = rowview["detailurl2"].ToString();

        }
        if ((rowview["detailurl3"].ToString() == "") || (rowview["detailurl3"].ToString() == null))
        {
            this.detailimgurl3.Visible = false;
        }
        else
        {
            this.detailimgurl3.ImageUrl = rowview["detailurl3"].ToString();

        }
        if ((rowview["detailurl4"].ToString() == "") || (rowview["detailurl4"].ToString() == null))
        {
            this.detailimgurl4.Visible = false;
        }
        else
        {
            this.detailimgurl4.ImageUrl = rowview["detailurl4"].ToString();

        }
        if ((rowview["detailurl5"].ToString() == "") || (rowview["detailurl5"].ToString() == null))
        {
            this.detailimgurl5.Visible = false;
        }
        else
        {
            this.detailimgurl5.ImageUrl = rowview["detailurl5"].ToString();

        } 
        //this.detailimgurl2.ImageUrl = rowview["detailurl2"].ToString();
        //this.detailimgurl3.ImageUrl = rowview["detailurl3"].ToString();
        //this.detailimgurl4.ImageUrl = rowview["detailurl4"].ToString();
        //this.detailimgurl5.ImageUrl = rowview["detailurl5"].ToString();

        MySqlCommand Comm = new MySqlCommand("update product_info set beclickedtotalnumber=" + productbeclicked + " where productnum='" + productid + "'", myConnection);
        Comm.ExecuteNonQuery();
        myConnection.Close();

        dlBind();

    }


    public void dlBind()
    {
        //string sourceen = Request.QueryString["productsourceen"];
        //string kinden = Request.QueryString["productkinden"];
        //string beclickedtotalnumber = Request.QueryString["beclickedtotalnumber"];
        string productid = Request.QueryString["productid"];

        int curpage = Convert.ToInt32(this.lblCurPage.Text);

        //先定义一个PageDataSource,分页全靠它
        PagedDataSource ps = new PagedDataSource();

        //取到连接字符串
        string propertiesPath = Server.MapPath("mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        //定义一个MySqlDataAdapter
        MySqlDataAdapter myAdapter = new MySqlDataAdapter();

        myAdapter = new MySqlDataAdapter("select * from comment_info where productnum='" + productid + "' order by floor desc", myConnection);

        DataSet ds = new DataSet();
        //填充数据
        myAdapter.Fill(ds, "mytable");
        ps.DataSource = ds.Tables["mytable"].DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 10;
        ps.CurrentPageIndex = curpage - 1;
        this.lbtnFirstPage.Enabled = true;
        this.lbtnUpPage.Enabled = true;
        this.lbtnDownPage.Enabled = true;
        this.lbtnLastPage.Enabled = true;
        if (curpage == 1)//已经是第一页
        {
            this.lbtnFirstPage.Enabled = false;
            this.lbtnUpPage.Enabled = false;
        }
        if (curpage == ps.PageCount)//已经是末页
        {
            this.lbtnLastPage.Enabled = false;
            this.lbtnDownPage.Enabled = false;
        }
        this.lblTotalPage.Text = Convert.ToString(ps.PageCount);
        this.commentlist.DataSource = ps;
        this.commentlist.DataBind();
        myConnection.Close();

    }

    protected void lbtnFirstPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = Convert.ToString(1);
        this.dlBind();
    }

    protected void lbtnUpPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = Convert.ToString(Convert.ToUInt32(this.lblCurPage.Text) - 1);
        this.dlBind();
    }
    protected void lbtnDownPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = Convert.ToString(Convert.ToUInt32(this.lblCurPage.Text) + 1);
        this.dlBind();
    }
    protected void lbtnLastPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = this.lblTotalPage.Text;
        this.dlBind();
    }

    protected string Substring(string str, int i, int j)
    {
        mytools mt = new mytools();
        string resultstr = mt.Substring(str, i, j);
        return resultstr;
    }

    protected string ReformDate(string addtime)
    {
        mytools mt = new mytools();
        string datereformed = mt.ReformDate(addtime);
        return datereformed;
    }

    protected void pinglun_Click(object sender, ImageClickEventArgs e)
    {

        //获取当前URL
        string currenturl = Request.Url.ToString();

        if (Convert.ToString(Session["username"]) == "")
        {
            
            Response.Write("<script lanuage=javascript>alert('评论前请先登录哦。');</script>");
            return;
        }

        if (newcomment.Text=="")
        {
            Response.Write("<script lanuage=javascript>alert('写点东西吧，评论内容不能为空哦。');</script>");
            return;
        }

        string productid = Request.QueryString["productid"];
        int maxfloor;

        //取到连接字符串
        string propertiesPath = Server.MapPath("mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        string sqlselectcommentcount = "select * from comment_info where productnum='" + productid + "'";
        MySqlDataAdapter da = new MySqlDataAdapter(sqlselectcommentcount, myConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "commentcount");

        //如果是第一次添加评论，floor为1
        if (ds.Tables["commentcount"].Rows.Count == 0)
        {
            maxfloor = 1;
        }
        //如果不是第一次添加评论，则floor为原来的floor值+1
        else
        {
            string sqlselectmax = "select max(floor) as maxfloor from comment_info where productnum='" + productid + "'";
            MySqlDataAdapter da2 = new MySqlDataAdapter(sqlselectmax, myConnection);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2, "maxfloor");
            DataRowView rowview = ds2.Tables["maxfloor"].DefaultView[0];
            maxfloor = Convert.ToInt32(rowview["maxfloor"]) + 1;
        }
                
        string adduser = Convert.ToString(Session["username"]);
        string content = newcomment.Text.ToString();
        DateTime dt = Convert.ToDateTime(DateTime.Now);
        string addtime = dt.ToString("yyyyMMddHHmmss");

        try
        {
            string sqlinsert = "insert into comment_info (adduser,productnum,floor,content,addtime)values" +
                "('" + adduser + "','" + productid + "','" + maxfloor + "','" + content + "','" + addtime + "')";
            MySqlCommand scd = new MySqlCommand(sqlinsert, myConnection);
            scd.ExecuteNonQuery();
            //刷新当前页
            Response.Write("<script lanuage=javascript>alert('添加评论成功！');location='" + currenturl + "'</script>");
            myConnection.Close();
        }
        catch (Exception err)
        {
            Response.Write(err.ToString());
            Response.Write("<script lanuage=javascript>alert('添加评论失败！');");
            //Response.Write("<script lanuage=javascript>alert('添加产品失败！');location ='add_item.aspx'</script>");
            myConnection.Close();
        }


    }
}
